package com.jintoufs.weixin;

import com.jintoufs.domain.sys.Dictionary;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.util.weixin.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.List;

public class AccessTokenAction extends SupportAction {
    @Resource
    private DictionaryService dictionaryService;

    @Value("${appId}")
    private String appId;
    @Value("${secret}")
    private String secret;
    @Value("${service_appId}")
    private String serviceAppId;
    @Value("${service_secret}")
    private String serviceSecret;

    /**
     * 獲取accesstoken
     */
    public void getMiniProgramAccessToken() throws Exception {
        String accessToken = AccessTokenUtil.getAccessToken(appId, secret);
        System.out.println("miniAccessToken:" + accessToken);
        List<Dictionary> dictionaryList = dictionaryService.getDicByKeyNo("miniProgram_accessToken");
        if (!dictionaryList.isEmpty()) {
            Dictionary dictionary = dictionaryList.get(0);
            dictionary.setCaption(accessToken);
            dictionaryService.update(dictionary);
        }
    }

    /**
     * 獲取accesstoken
     */
    public void getServiceAccessToken() throws Exception {
        String accessToken = AccessTokenUtil.getAccessToken(serviceAppId, serviceSecret);
        System.out.println("ServiceAccessToken:" + accessToken);
        List<Dictionary> dictionaryList = dictionaryService.getDicByKeyNo("service_accessToken");
        if (!dictionaryList.isEmpty()) {
            Dictionary dictionary = dictionaryList.get(0);
            dictionary.setCaption(accessToken);
            dictionaryService.update(dictionary);
        }
    }
}